class Solution {
public:
    int reversePairs(vector<int>& nums) 
    {
        vector<int>help(nums.size());
        return mergesort(nums,help,0,nums.size()-1);
    }
    int mergesort(vector<int>&nums,vector<int>&help,int left,int right)
    {
        if(left>=right) return 0;
        int mid=(right-left)/2+left;
        int ret=0;
        ret+=mergesort(nums,help,left,mid);
        ret+=mergesort(nums,help,mid+1,right);
        int begin1=left,end1=mid,begin2=mid+1,end2=right,index=left;
        for(int i=left,j=mid+1;i<=mid;i++)
        {
           while(j<=right&&nums[i]<=(long long int)nums[j]*2) j++;
           ret+=right-j+1;
        }
        while(begin1<=end1&&begin2<=end2)
        {
            if(nums[begin1]>=nums[begin2])
            {
                
                help[index++]=nums[begin1++];
            }
            else
            {
                help[index++]=nums[begin2++];
            }
        }
        while(begin1<=end1) help[index++]=nums[begin1++];
        while(begin2<=end2)help[index++]=nums[begin2++];
        for(int i=left;i<=right;i++)
        {
            nums[i]=help[i];
        }
        return ret;
    }
};